local extension = Package("fengqi_fire")
extension.extensionName = "aaa_fengqi"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["fengqi_fire"] = "火",
}


local yuanshao = General(extension, "efengqi__yuanshao", "qun", 4)

local efengqi__qunni = fk.CreateViewAsSkill{
  name = "efengqi__qunni",
  anim_type = "offensive",
  pattern = "wild_revolt",
  prompt = "#efengqi__qunni",
  card_filter = function(self, to_select, selected)
    return #selected < 2
  end,
  view_as = function(self, cards)
    if #cards ~= 2 then return nil end
    local c = Fk:cloneCard("wild_revolt")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  before_use = function (self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data.efengqi__qunniFrom = player.id
  end,
}

local efengqi__qunni_trigger = fk.CreateTriggerSkill{
  name = "#efengqi__qunni_trigger",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.Deathed},
  main_skill = efengqi__qunni,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and data.damage and data.damage.from == player and data.damage.card
    and table.contains(data.damage.card.skillNames, "efengqi__qunni")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pile = room:getBanner("@$CenterArea") or {}
    if #pile > 0 then
      room:obtainCard(player, table.random(pile), true, fk.ReasonJustMove, player.id, "efengqi__qunni")
    end
  end,

  refresh_events = {fk.PreDamage},
  can_refresh = function (self, event, target, player, data)
    if data.card and table.contains(data.card.skillNames, "efengqi__qunni") then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if e then
        local use = e.data[1]
        return use.card == data.card and use.extra_data and use.extra_data.efengqi__qunniFrom == player.id
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    data.from = player
  end,
}
efengqi__qunni:addRelatedSkill(efengqi__qunni_trigger)

efengqi__qunni.CenterArea = true
yuanshao:addSkill(efengqi__qunni)

local efengqi__zongyi = fk.CreateTriggerSkill{
  name = "efengqi__zongyi", -- 主公技无法检测隐匿……
  anim_type = "control",
  events = {fk.BuryVictim},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.damage and data.damage.from == player and target.rest == 0 and target.role == "loyalist"
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.skip_reward_punish = true
  end,
}
efengqi__zongyi.isHiddenSkill = true
yuanshao:addSkill(efengqi__zongyi)

table.insert(Fk.lords, "efengqi__yuanshao")

Fk:loadTranslationTable{
  ["efengqi__yuanshao"] = "袁绍",
  ["#efengqi__yuanshao"] = "高贵的名门",
  ["designer:efengqi__yuanshao"] = "隐匿",
  ["illustrator:efengqi__yuanshao"] = "",

  ["efengqi__qunni"] = "群逆",
  [":efengqi__qunni"] = "你可以将两张牌当【群逆纵逸】使用，且你视为伤害来源；你因此杀死角色后，你获得中央区一张牌。",
  ["#efengqi__qunni"] = "群逆：将两张牌当【群逆纵逸】使用，且你视为伤害来源",
  ["#efengqi__qunni_trigger"] = "群逆",

  ["efengqi__zongyi"] = "纵逸",
  [":efengqi__zongyi"] = "隐匿，你杀死忠臣后无须执行奖惩。",

  ["$efengqi__qunni1"] = "弓箭手，准备放箭！",
  ["$efengqi__qunni2"] = "全都去死吧！",
  ["~efengqi__yuanshao"] = "孟德此计，防不胜防……",
}

local taishici = General:new(extension, "efengqi__taishici", "wu", 4)
Fk:loadTranslationTable{
  ["efengqi__taishici"] = "太史慈",
  ["#efengqi__taishici"] = "勇撼日月",
  ["designer:efengqi__taishici"] = "祭祀",
  ["illustrator:efengqi__taishici"] = "鱼仔",
}

local hanzhan = fk.CreateActiveSkill{
  name = "efengqi__hanzhan",
  anim_type = "offensive",
  prompt = "#efengqi__hanzhan",
  card_num = 0,
  min_target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, _, _, _, player)
    local to = Fk:currentRoom():getPlayerById(to_select)
    local card = Fk:cloneCard("duel")
    card.skillName = self.name
    return player.id ~= to_select and to:canUseTo(card, player)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:sortPlayersByAction(effect.tos)
    local tos = table.map(effect.tos, Util.Id2PlayerMapper)
    local victims = {}
    for _, to in ipairs(tos) do
      if player.dead then break end
      if not to.dead then
        local use = room:useVirtualCard("duel", nil, to, player, self.name)
        if not (use and use.damageDealt and use.damageDealt[player.id]) then
          table.insert(victims, to)
        end
      end
    end
    if player.dead then return end
    victims = table.filter(victims, function(v) return not v.dead end)
    if #victims > 0 then
      room:useVirtualCard("slash", nil, player, victims, self.name, true)
    end
  end,
}
taishici:addSkill(hanzhan)

Fk:loadTranslationTable{
  ["efengqi__hanzhan"] = "酣战",
  [":efengqi__hanzhan"] = "出牌阶段限一次，你可以令任意名其他角色依次视为对你使用一张【决斗】，然后你视为对未对你造成伤害的角色使用【杀】。",
  ["#efengqi__hanzhan"] = "酣战：令任意名角色视为对你使用【决斗】，再视为对未对你造成伤害的角色使用【杀】。",
}

local dulie = fk.CreateTriggerSkill{
  name = "efengqi__dulie",
  anim_type = "offensive",
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and data.card.type == Card.TypeTrick then
      local from = player.room:getPlayerById(data.from)
      return from and from ~= player and player:canPindian(from)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#efengqi__dulie-ask:"..data.from) then
      self.cost_data = {tos = {data.from}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = room:getPlayerById(data.from)
    local pindian = player:pindian({from}, self.name)
    if pindian and pindian.results[from.id] then
      local winner = pindian.results[from.id].winner
      if winner then
        if not winner.dead then
          winner:drawCards(2, self.name)
        end
        if winner == player then
          room:addPlayerMark(player, MarkEnum.SlashResidue.."-phase")
        end
      end
    end
  end,
}
taishici:addSkill(dulie)
Fk:loadTranslationTable{
  ["efengqi__dulie"] = "笃烈",
  [":efengqi__dulie"] = "你成为其他角色的锦囊牌的目标后，你可以与其拼点，赢者摸两张牌，若为你，你本阶段出【杀】次数+1。",
  ["#efengqi__dulie-ask"] = "笃烈：你可以与 %src 拼点，赢者摸两张牌，若为你，你本阶段出【杀】次数+1。",
}


local pangtong = General:new(extension, "efengqi__pangtong", "shu", 3)
Fk:loadTranslationTable{
  ["efengqi__pangtong"] = "庞统",
  ["#efengqi__pangtong"] = "不死鸟",
  ["designer:efengqi__pangtong"] = "zzcclll朱苦力",
  ["illustrator:efengqi__pangtong"] = "",
  ["~efengqi__pangtong"] = "看来我命中注定将丧命于此……",
}

local lianhuan = fk.CreateActiveSkill{
  name = "efengqi__lianhuan",
  anim_type = "control",
  card_num = 1,
  target_num = 1,
  prompt = "#efengqi__lianhuan",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).suit == Card.Club
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0
  end,
  can_use = function(self, player)
    return true
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local chained = not to.chained
    room:recastCard(effect.cards, player, self.name)
    if not to.dead then
      to:setChainState(chained)
    end
  end,
}
local lianhuan_trigger = fk.CreateTriggerSkill{
  name = "#efengqi__lianhuan_trigger",
  mute = true,
  main_skill = lianhuan,
  events = {fk.ChainStateChanged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lianhuan) and target == player and not player.chained
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:broadcastSkillInvoke(lianhuan.name)
    player.room:notifySkillInvoked(player, lianhuan.name, "drawcard")
    player:drawCards(1, lianhuan.name)
  end,
}
lianhuan:addRelatedSkill(lianhuan_trigger)

pangtong:addSkill(lianhuan)

Fk:loadTranslationTable{
  ["efengqi__lianhuan"] = "连环",
  [":efengqi__lianhuan"] = "出牌阶段，你可以重铸一张♣牌并横置或重置一名角色；你脱离连环状态时，摸一张牌。",
  ["#efengqi__lianhuan"] = "连环：重铸一张♣牌并横置或重置一名角色",

  ["$efengqi__lianhuan1"] = "伤一敌可连其百！",
  ["$efengqi__lianhuan2"] = "通通连起来吧！",
}

local niepan = fk.CreateTriggerSkill{
  name = "efengqi__niepan",
  anim_type = "offensive",
  events = {fk.EventPhaseStart, fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Finish
      else
        return data.damage and data.damage.damageType == fk.FireDamage
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.EnterDying then return true end
    return player.room:askForSkillInvoke(player, self.name, nil, "#efengqi__niepan-invoke")
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:damage { from = player, to = player, damage = 1, skillName = self.name, damageType = fk.FireDamage }
    else
      room:recover { num = player.maxHp - player.hp, skillName = self.name, who = player, recoverBy = player }
    end
  end,
}
pangtong:addSkill(niepan)

Fk:loadTranslationTable{
  ["efengqi__niepan"] = "涅槃",
  [":efengqi__niepan"] = "结束阶段，你可以受到1点火焰伤害；你因火焰伤害进入濒死状态时，回复体力至上限。",
  ["#efengqi__niepan-invoke"] = "涅槃:你可以受到1点火焰伤害",

  ["$efengqi__niepan1"] = "凤雏岂能消亡？",
  ["$efengqi__niepan2"] = "浴火重生！",
}


return extension
